package com.app.micaihu.view.user.userinfo.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;

import com.android.volley.VolleyError;
import com.app.config.LoadViewConfig;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.comment.MyComment;
import com.app.micaihu.bean.infor.UserHomePage;
import com.app.micaihu.bean.infor.UserHomePageNavBean;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.HttpUtils;
import com.app.micaihu.view.user.userinfo.adapter.MyDynamicAdapter2052;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 我的主页-动态
 * Created by linkai on 2016/3/14.
 */
public class DynamicPageFragment extends HeaderViewBaseFragment<MyComment>{


    private boolean isMain;

    private String toUid;//要查看的用户id

    private MyDynamicAdapter2052 mAdapter;
    protected static final String ARG_TOUID = "toUid";
    protected static final String ARG_OBJECT = "object";
    private UserHomePageNavBean navBean;

    public DynamicPageFragment() {
    }



    public static DynamicPageFragment newInstance(UserHomePageNavBean navBean, String toUid) {

        DynamicPageFragment fragment = new DynamicPageFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TOUID, toUid);
        args.putSerializable(ARG_OBJECT, navBean);
        fragment.setArguments(args);
        return fragment;
    }
    public void initData(List<MyComment> list) {
        if (list != null) {
            if (mDatalist == null) {
                mDatalist = new ArrayList<>();
            }
            removeOld(list);
            mDatalist.addAll(list);


        }
    }


    /**
     * 去除 老版话题.
     *
     * @param list
     * @return
     */
    private List<MyComment> removeOld(List<MyComment> list) {
        if (list == null) {
            list = new ArrayList<MyComment>();
        }
        if (list.isEmpty()) {
            return list;
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == null) {
                list.remove(i);
                i--;
            } else {
                if (TextUtils.isEmpty(TypeConstant.getNewsTypeName(list.get(i).getArticleType()))) {
                    list.remove(i);
                    i--;
                }
            }
        }
        return list;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        toUid = getArguments().getString(ARG_TOUID);
        navBean = (UserHomePageNavBean) getArguments().getSerializable(ARG_OBJECT);



        if (UserInforManager.getInstance().isLogin()) {
            isMain = TextUtils.equals(UserInforManager.getInstance().getUserInfor().getUid(), toUid);

        }

        if (mDatalist == null || mDatalist.size() <= 0) {
            loadData(true);
        } else {
            if (mDatalist.size() > 0 && mListView != null) {
                if (mAdapter == null) {
                    mAdapter = new MyDynamicAdapter2052(mDatalist, getActivity(), isMain);
                    try {
                        mListView.setAdapter(mAdapter);
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    }
                } else {
                    mListView.setAdapter(mAdapter);
                    mAdapter.notifyDataSetChanged();
                }
                onComplete(1, null);
            } else {
                onComplete(3, AppApplication.getInstance().getString(R.string.my_home_empty_dyanmic));
            }

        }
    }

    /**
     * 加载数据
     *
     * @param isRefresh
     */
    @Override
    public void loadData(final boolean isRefresh) {

        HashMap<String, String> parms = new HashMap<>();
        String type = "2";//1 为个人首页  2为其他人首页
        if (UserInforManager.getInstance().isLogin()) {
            parms.put("uid", UserInforManager.getInstance().getUserInfor().getUid());
            if (TextUtils.equals(toUid, UserInforManager.getInstance().getUserInfor().getUid())) {
                type = "1";
            } else {
                type = "2";
            }


        }

        parms.put("toUid", toUid);
        parms.put("page", page + "");
        parms.put("type", type);
        HttpListener<DataBean<UserHomePage>> httpListener = new HttpListener<DataBean<UserHomePage>>() {

            @Override
            public void onStart() {
                //设置正在刷新
                onRefreshing(null);
            }

            @Override
            public void onError(VolleyError error) {
                //设置刷新状态
//                onComplete(2,null);
                if (mDatalist == null || mDatalist.isEmpty()) {
                    mListView.setEmptyView(lvLoading);
                    lvLoading.showErrorPage();
                    isRefreshing = false;
                } else {
                    onComplete(2, null);

                }
            }

            @Override
            public void onSuccess(DataBean<UserHomePage> response) {
                isRefreshing = false;
                if (response.noError() && response.getData() != null) {
                    List<MyComment> list = response.getData().getCmtList();


                    if (list.size() > 0) {
                        removeOld(list);
                        if (mDatalist == null) {
                            mDatalist = new ArrayList<>();
                        }
                        if (isRefresh) {
                            mDatalist.clear();
                        }
                        mDatalist.addAll(list);
                        if (mAdapter == null) {
                            mAdapter = new MyDynamicAdapter2052(mDatalist, getActivity(), isMain);
                            mListView.setAdapter(mAdapter);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }

                        onComplete(1, "");//展示底部空view
                    } else {
                        if (mDatalist == null || mDatalist.isEmpty()) {//展示空view
                            mListView.setEmptyView(lvLoading);
                            lvLoading.showEmptyPage(LoadViewConfig.EMPTY_DEFAULT, AppApplication.getInstance().getString(R.string.my_home_empty_dyanmic));
                            isRefreshing = false;

                        } else {
                            onComplete(2, AppApplication.getInstance().getString(R.string.pullup_nomore_data));//展示底部空view
                        }
                    }
                } else {
                    if (mDatalist == null || mDatalist.isEmpty()) {//展示errorPage
                        mListView.setEmptyView(lvLoading);

                        lvLoading.showErrorPage();
                        isRefreshing = false;
                    } else {
                        onComplete(2, AppApplication.getInstance().getString(R.string.pullup_nomore_data));
                    }
                }

            }
        };
        HttpUtils.addPostRequest(URIContant.MY_HOMEN_PAGE, new TypeToken<DataBean<UserHomePage>>() {
        }.getType(), "", parms, httpListener);
    }






}
